/**
 * 控制器
 */
class Controller extends egret.DisplayObjectContainer {
    public constructor() {
        super()
        this.once(egret.Event.ADDED_TO_STAGE, this.init, this)
    }
    private init() {
        const miva = this

        miva.btnGroupTop()
        miva.createScrollViewMid()
        miva.createBtnBtm()
    }
    // 上方按钮组 
    private btnGroupTop() {
        const miva = this
        const container = new egret.Sprite()

        container.width = miva.stage.stageWidth * .8
        container.anchorOffsetX = container.width * .5
        container.x = miva.stage.stageWidth * .5

        miva.btnSwitchSex(container)
        miva.btnFinish(container)
        miva.addChild(container)
    }
    // 性别切换按钮
    private btnSwitchSex(container: egret.DisplayObjectContainer) {
        const miva = this
        const btn = Utils.createBitmapByName('btn_switch_sex_png')
        btn.touchEnabled = true
        btn.addEventListener(egret.TouchEvent.TOUCH_TAP, _ => {
            miva.parent.dispatchEvent(new egret.Event('SWITCH_SEX'))
            miva.parent.dispatchEvent(new egret.Event('SWITCH_ITEM_MID'))
        }, miva)
        container.addChild(btn)
    }
    // 完成按钮
    private btnFinish(container: egret.DisplayObjectContainer) {
        const miva = this
        const btn = Utils.createBitmapByName('btn_finish_png')
        btn.touchEnabled = true
        btn.addEventListener(egret.TouchEvent.TOUCH_TAP, _ => {
            miva.stage.dispatchEvent(new egret.Event('FINISH'))
        }, miva)

        btn.anchorOffsetX = btn.width
        btn.x = container.width

        container.addChild(btn)
    }
    // 中层滚动
    private createScrollViewMid() {
        const miva = this
        const container = new egret.Sprite()
        const bg_png = Utils.createBitmapByName('bg_item_container_png')
        const scroll = new eui.Scroller()

        scroll.width = bg_png.width
        scroll.height = bg_png.height

        container.y = miva.height + 30

        container.addChild(bg_png)
        container.addChild(scroll)
        miva.addChild(container)

        settingGroup()

        miva.parent.addEventListener('SWITCH_ITEM_MID', settingGroup, miva)

        function settingGroup() {
            const group = new eui.Group()
            group.height = bg_png.height
            scroll.viewport = group
            miva.createItemMid(group)
        }
    }
    // 创建中层道具
    private createItemMid(container: egret.DisplayObjectContainer) {
        const miva = this
        // 确定使用的道具组
        const useGroup = ParamsPool.USE_GROUP_ID !== 2
            ? ParamsPool.DATA_ITEM[`group_item_${ParamsPool.USE_GROUP_ID}`]
            : ParamsPool.DATA_ITEM[`group_item_${ParamsPool.USE_GROUP_ID}`][ParamsPool.SEX]

        for (let i = 1; i <= useGroup.length; i++) {
            const _container = new eui.Group()
            const nowItemGroup = useGroup[i - 1]
            const item = Utils.createBitmapByName(`${nowItemGroup.icon}_png`)

            _container.addChild(item)
            container.addChild(_container)

            Utils.anchorCenter(item)

            _container.height = _container.width = container.height
            _container.x = _container.width * (i - 1)
            item.y = item.x = _container.width * .5

            item.touchEnabled = true
            item.addEventListener(egret.TouchEvent.TOUCH_TAP, _ => {
                switch (ParamsPool.USE_GROUP_ID) {
                    case 1:
                        miva.stage.dispatchEventWith('SWITCH_BG', false, nowItemGroup.item, false)
                        break;
                    case 2:
                        miva.stage.dispatchEventWith('SWITCH_YF', false, {
                            item: nowItemGroup.item,
                            type: nowItemGroup.type
                        }, false)
                        break
                    default:
                        const _item = new DragItemGenerator(`${nowItemGroup.item}_png`)
                        ScreenGame.Main.addChild(_item)
                        miva.stage.dispatchEvent(new egret.Event('CREATE_ITEM'))
                }
            }, miva)
        }

    }
    // 下方按钮组
    private createBtnBtm() {
        const miva = this
        const container = new egret.Sprite()
        container.width = miva.stage.stageWidth
        for (let i = 1; i <= 5; i++) {
            const btn = new egret.Sprite()

            drawBtnBgc(btn, i)

            const icon = Utils.createBitmapByName(`btn_0${i}_png`)
            Utils.anchorCenter(icon)
            icon.x = btn.x + (btn.width * .5)
            icon.y = btn.height * .5

            btn.x = container.width * .2 * (i - 1)

            btn.addChild(icon)
            container.addChild(btn)
            btnTouchEvent(btn, i)

            miva.parent.addEventListener('SWITCH_ITEM_MID', _ => {
                drawBtnBgc(btn, i)
            }, miva)
        }
        container.y = miva.height
        miva.addChild(container)

        // 按钮事件绑定
        function btnTouchEvent(btn: egret.Sprite, i) {
            btn.touchEnabled = true
            btn.addEventListener(egret.TouchEvent.TOUCH_TAP, _ => {
                ParamsPool.USE_GROUP_ID = i
                miva.parent.dispatchEvent(new egret.Event('SWITCH_ITEM_MID'))
            }, miva)
        }
        // 绘制按钮底色
        function drawBtnBgc(btn: egret.Sprite, i) {
            btn.graphics.clear()
            btn.graphics.lineStyle(2, 0xffffff)
            ParamsPool.USE_GROUP_ID !== i
                ? btn.graphics.beginFill(0xc56d4f, 1)
                : btn.graphics.beginFill(0x7f5a4c, 1)
            btn.graphics.drawRect(0, 0, container.width * .2, 50)
            btn.graphics.endFill()
        }
    }
}
